<?php

namespace app\api\controller;

use think\Db;
use tool\Price;

class Tast extends Base
{

    public function do_refund()
    {
        set_time_limit(0);
        $app = $this->getwxapp("pay");
        $list = model("order")->where("status", 4)->where("refund_time", 0)->order("time asc")->limit(10)->select();
        foreach ($list as $order) {
            $result = $app->refund->byOutTradeNumber($order['orderno'], $order['orderno'], intval($order['totalprice']*100), intval($order['totalprice']*100));
            if (!empty($result['return_code']) && !empty($result['result_code']) && $result['return_code'] === "SUCCESS" && $result['result_code'] === "SUCCESS") {
                model("order")->where("orderno",$order['orderno'])->update(["refund_time"=>getMillisecond()]);
            }
        }
    }

    public function do_pt(){
        $time=time();
        Db::query("update pt_group set status=2 where cnumber< number and end_timestamp <{$time} and  status=0");
        $refund_group_list=model("ptGroup")->where("optime",0)->where("status",2)->select();
        foreach ($refund_group_list as  $refund_group){
            $group_id=$refund_group['id'];
            $pt_order_lists=model("ptOrder")->where("pt_group_id",$group_id)->select();
            foreach ($pt_order_lists as $pt_order){
                $this->refund($pt_order['orderno']);
            }
            model("ptGroup")->where("id", $group_id)->update(['optime'=>getMillisecond()]);
        }

        $finish_group_list=model("ptGroup")->where("optime",0)->where("status",1)->select();
        foreach ($finish_group_list as  $finish_group){
            $group_id=$finish_group['id'];
            $pt_order_lists=model("ptOrder")->where("pt_group_id",$group_id)->select();
            foreach ($pt_order_lists as $pt_order){
                model("order")->where("orderno",$pt_order['orderno'])->update(['isused'=>1]);
            }
            model("ptGroup")->where("id", $group_id)->update(['optime'=>getMillisecond()]);
        }

        return "success";
    }

    

}
